// set集合类型---和数组最大的区别在于数组内容可以重复，但是集合不行
let arr = [1, 2, 44, 5, 66, 1, 1, 1];
let set = new Set([1, 2, 3, 4, 4, 4]);
let set2 = new Set('123456789');
let set3  = new Set([...set2].filter(item => item < 6));
let arr2 = [...set];
console.log('set集合类型:', arr, set, Array.from(set), set2, set3, arr2);

// 通过set结构取存储用户输入的数据并且给予提示列表----完美解决数据重复问题
let dataObj = {
    data: new Set(),
    set keyword(value) {
        this.data.add(value);
    },
    show: function() {
        let ul = document.querySelector("[name='hd']");
        ul.innerHTML = '';
        this.data.forEach(item => {
            console.log('item=>', item);
            ul.innerHTML += `<li>${item}</li>` 
        });
    }
};
let input = document.querySelector("#value");
input.addEventListener('blur', function() {
    dataObj.keyword = this.value;
    dataObj.show();
});


// set----并集、差集、交集
//  并集
let setA = new Set([1, 2, 3]);
let setB = new Set([4, 5, 6]);
let collectAB = new Set([...setA, ...setB]);
console.log('set并集：', collectAB);
// 交集
let setC = new Set([1, 2, 3, 5, 8]);
let setD = new Set([2, 8, 9, 99]);
let uniCD = new Set([...setC].filter(item => setD.has(item)));
console.log('set-交集：', uniCD);
// 差集
let diffCD = new Set([...setC, ...setD].filter(item => !setD.has(item) || !setC.has(item)));
console.log('set-差集：', diffCD);

// weakSet弱引用（引用计数器不会增加减少）set --- 必须是引用类型
let a = {name: 'shansan'};
let weakset = new WeakSet();
// weakset.delete(a);
weakset.add(a);
console.log('weakSet弱引用set-before:', a, weakset);
a = null;
console.log('weakSet弱引用set-after:', a, weakset);
